package com.suixinsanren.design.command.demo01;

public class RemoteCommand {

    private Command[] onCommands;
    private Command[] offCommands;
    private Command undo;

    public RemoteCommand(int slot) {
        onCommands = new Command[slot];
        offCommands = new Command[slot];
        undo = new NoCommand();
    }

    public void setCommand(int slot, Command onCommand, Command offCommand) {
        slotCheck(onCommands, slot);
        slotCheck(offCommands, slot);
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

    public void onButtonWasPushed(int slot) {
        slotCheck(onCommands, slot);
        undo = onCommands[slot];
        undo.execute();
    }

    public void offButtonWasPushed(int slot) {
        slotCheck(offCommands, slot);
        undo = offCommands[slot];
        undo.execute();
    }

    public void undo(){
        undo.undo();
    }

    private void slotCheck(Command[] commands, int slot) {
        if (slot < 0 || slot >= commands.length) {
            throw new IllegalArgumentException(slot + "非法值");
        }
    }
}
